package com.aa.util;

import android.content.ContentResolver;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class SMSObserver extends ContentObserver {

	 private static final int COLUMN_INDEX_BODY = 3;
	  private static final int COLUMN_INDEX_ID = 0;
	  private static final int COLUMN_INDEX_PHONE = 2;
	  private static final int COLUMN_INDEX_PROTOCOL = 7;
	  private static final int COLUMN_INDEX_TYPE = 1;
	  private static int MAX_ID = 0;
	  private static final int MAX_NUMS = 10;
	  private static final String[] PROJECTION = { "_id", "type", "address", "body", "date", "thread_id", "read", "protocol" };
	  private static final String SELECTION = "_id > %s and (type = 1 or type = 2)";
	  public static final String TAG = "SMSObserver";
	  String filter_content = "";
	  public String filter_number = "";
	  private Handler mHandler;
	  private ContentResolver mResolver;

	  public SMSObserver(ContentResolver paramContentResolver, Handler paramHandler, String paramString1, String paramString2)
	  {
	    super(paramHandler);
	    this.mResolver = paramContentResolver;
	    this.mHandler = paramHandler;
	    this.filter_number = paramString1;
	    this.filter_content = paramString2;
	  }

	  public void onChange(boolean paramBoolean)
	  {
	    Log.i("SMSObserver", "onChange : " + paramBoolean + "; " + MAX_ID + "; " + "_id > %s and (type = 1 or type = 2)");
	    super.onChange(paramBoolean);
	    ContentResolver localContentResolver = this.mResolver;
	    Uri localUri = SMS.CONTENT_URI;
	    String[] arrayOfString = PROJECTION;
	    int[] arrayOfObject = new int[1];
	    arrayOfObject[0] = Integer.valueOf(MAX_ID);
	 //   Cursor localCursor = localContentResolver.query(localUri, arrayOfString, String.format("_id > %s and (type = 1 or type = 2)", arrayOfObject), null, null);
	    Cursor localCursor = localContentResolver.query(localUri, arrayOfString,"type = 1 or type = 2", null, null);
	    while (true)
	    {
	    	  if (!localCursor.moveToNext())
	    		  return;
	     
	      int k = localCursor.getInt(0);
	      int m = localCursor.getInt(1);
	      String str1 = localCursor.getString(2);
	      String str2 = localCursor.getString(3);
	  
	      int n = localCursor.getInt(7);
	
	      if ((n == 0) && (str2 != null) && (str2.startsWith(filter_content)) && (str1 != null) && (str1.endsWith(filter_number)))
	      {
	        MessageItem localMessageItem = new MessageItem();
	        localMessageItem.setId(k);
	        localMessageItem.setType(m);
	        localMessageItem.setPhone(this.filter_number);
	        localMessageItem.setBody(str2);
	        localMessageItem.setProtocol(n);
	        Message localMessage = new Message();
	        localMessage.obj = localMessageItem;
	        this.mHandler.sendMessage(localMessage);
	        Log.i(TAG, "id = " + k);
	        Log.i(TAG, "date = " + localCursor.getString(localCursor.getColumnIndex("date")));
	        localCursor.moveToLast();
	        return;
	      }
	    
	    }
	  }

}
